//
//  XQNavigationController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQNavigationController.h"

@interface XQNavigationController()<UIGestureRecognizerDelegate>

@end

@implementation XQNavigationController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {
        [self.interactivePopGestureRecognizer setDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setHidden:YES];
    self.view.backgroundColor = [UIColor clearColor];
    
    if (@available(iOS 13, *)) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
}

#pragma mark - Present

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if ([NSThread isMainThread]) { // 系统弹 alert 会走这里，导致在异步线程闪退
        [super presentViewController:viewControllerToPresent animated:flag completion:completion];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [super presentViewController:viewControllerToPresent animated:flag completion:completion];
        });
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count <= 1) {
        return NO;
    }
    id vc = self.topViewController;
    if (vc && [vc respondsToSelector:@selector(xq_navigationRecognizerShouldBegin:)]) {
        return [vc xq_navigationRecognizerShouldBegin:gestureRecognizer];
    }
    return YES;
}

- (void)navigationController:(UINavigationController *)navigationController
        willShowViewController:(UIViewController *)viewController
        animated:(BOOL)animated {

    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    /* We don't need Edit button in More screen. */
    morenavitem.rightBarButtonItem = nil;
}




@end
